import { getControlModel } from '@/api/statusInfo.js'
import { changeControlModel } from '@/api/cruiseMange.js'
import { getPlansList } from '@/api/planManage.js'

export default {
    state: {
        shipState: '- - -',//无人船状态
        recordAble: false,//无人船记录是否开启
        plansList: [],//计划列表数据
        sensorLocation: JSON.parse(localStorage.getItem('sensorLocation') || '{}'),//传感器位置
    },
    actions: {
        // 获取无人船状态（包括状态，当前执行的计划，记录是否开启）
        toGetControlModel({ commit }) {
            getControlModel().then(res => {
                commit("setControlModel", res.boatState);
            }).catch(err => {
                console.log(err);
            })
        },
        // 修改无人船状态
        toChangeControlModel({ dispatch }, target) {
            changeControlModel(target).then(() => {
                // 修改了无人船状态之后，重新获取一次状态
                dispatch('toGetControlModel');
            }).catch(err => {
                console.log(err);
            })
        },
        // 获取无人船计划列表
        toGetPlansList({ commit }) {
            getPlansList().then(res => {
                commit('setPlansList', res)
            }).catch(err => {
                console.log(err);
            })
        }
    },
    mutations: {
        // 存放无人船状态数据
        setControlModel(state, value) {
            // 对状态进行转换为中文
            switch (value.state) {
                case 'standby':
                    value.state = '待机中'; break;
                case 'automatic':
                    value.state = '自动巡航'; break;
                case 'telecontrol':
                    value.state = '手动遥控'; break;
            }
            state.shipState = value.state;//储存当前的无人船状态
            state.recordAble = value.startRecord == 1 ? true : false;//储存当前无人船有无开启记录
        },
        // 断开连接需要让数据复原
        clearData(state) {
            state.shipState = '- - -';
            state.recordAble = false
        },
        // 存放无人船计划列表数据
        setPlansList(state, listData) {
            state.plansList = listData;
        },
        // 存放传感器位置坐标
        setSensorLocation(state, data) {
            state.sensorLocation = data;
            localStorage.setItem("sensorLocation", JSON.stringify(data));
        }
    },
    getters: {
        toDoPlan(state) {
            return state.shipState.plan || '暂无';
        },
        sensorAddress(state) {
            return state.sensorLocation.address || '待加载';
        },
        sensorLnglat(state) {
            let lnglat = state.sensorLocation.longitude + ',' + state.sensorLocation.latitude;
            return lnglat || '待加载';
        }
    }
}